package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.AddressBean;
import com.wude.bean.UserInfo;
import com.wude.guild.AddAddressActivity;
import com.wude.guild.R;
import com.wude.guild.dialog.MillionDialog;
import com.wude.guild.fragment.ShouHuoFragment;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 收货地址Holder
 * Created by Administrator on 2017/3/21.
 */
@SuppressLint("HandlerLeak")
public class AddressHolder extends BaseHolder<AddressBean> {
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.add_checkbox)
    CheckBox addCheckbox;
    @BindView(R.id.check_text)
    TextView checkText;
    @BindView(R.id.bainji)
    RelativeLayout bainji;
    @BindView(R.id.delete)
    RelativeLayout delete;
    private UserInfo loginUser;
    private AddressBean addressBean;
    private int anInt;
    private ShouHuoFragment fragment;

    public AddressHolder(ShouHuoFragment fragment) {
        super();
        this.fragment = fragment;
    }

    @Override
    protected void refreshView(AddressBean addressBean, int position, Activity activity) {
        this.addressBean = addressBean;
        anInt = position;
        name.setText(addressBean.name);
        phone.setText(addressBean.phone);
        address.setText(addressBean.shiqu + "-" + addressBean.address);
        if (position == 0) {
            addCheckbox.setChecked(true);
            checkText.setText("默认地址");
        } else {
            addCheckbox.setChecked(false);
            checkText.setText("设为默认");
        }
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_address, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        addCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    AddressChage();
                } else {
                    if (anInt == 0) {
                        addCheckbox.setChecked(true);
                    }
                }
            }
        });
        return inflate;
    }

    /**
     * 默认地址发生改变
     */
    private void AddressChage() {
        loginUser = Utils.getLoginUser();
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        map.put("id", addressBean.id + "");
        HttpCom.POST(handler, HttpCom.getChageAddressURL(), map, false);
    }

    @OnClick({R.id.bainji, R.id.delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bainji:
                Edit();
                break;
            case R.id.delete:
                DeleteAddress();
                break;
        }
    }

    /**
     * 编辑
     */
    private void Edit() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("key", addressBean);
        intent.putExtras(bundle);
        intent.setClass(fragment.getContext(), AddAddressActivity.class);
        fragment.startActivity(intent);
    }

    /**
     * 删除收货地址
     */
    private void DeleteAddress() {
        final MillionDialog millionDialog = new MillionDialog(fragment.getContext(), R.style.MillionDialogStyle);
        millionDialog.setTitle("确定要删除该地址吗");
        millionDialog.setContext("");
        millionDialog.setBool(false);
        millionDialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                millionDialog.dismiss();
//                chuliDialog = new ChuliDialog(activity, R.style.MyDialogchuli);
//                chuliDialog.show();
                loginUser = Utils.getLoginUser();
                HashMap<String, String> map = new HashMap<>();
                map.put("token", loginUser.token);
                map.put("id", addressBean.id + "");
                HttpCom.POST(mhandler, HttpCom.getDelAddressURL(), map, false);
            }
        });
        millionDialog.show();
    }

    /**
     * 改变默认收货地址
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//            chuliDialog.dismiss();
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("改变收货地址json", msg.obj.toString());
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        if (status == 1) {
                            fragment.initdata();
                        } else {
                            String return_code = jsonObject.getString("return_code");
                            Log.e("改变收货地址返回码", return_code);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
    /**
     * 删除收货地址m
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//            chuliDialog.dismiss();
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("删除收货地址json", msg.obj.toString());
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        if (status == 1) {
                            fragment.initdata();
                        } else {
                            String return_code = jsonObject.getString("return_code");
                            Log.e("删除收货地址返回码", return_code);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
}
